/**
 * 报表设计公司(2011)
 */
package net.abbp.report.model.cell.style.background;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Shape;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.reportgear.report.core.api.compare.ComparatorUtils;

/**
 * @version 1.0 2011-3-24
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ANNCSR 2.0
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Image")
public class ImageBackground extends AbstractBackground {

    private static final Logger logger = LoggerFactory.getLogger(ImageBackground.class.getName());
    private transient Image image;
    private int layout;
    private double specifiedImageWidth;
    private double specifiedImageHeight;

    public ImageBackground() {
        this(null);
    }

    public ImageBackground(Image image) {
        this(image, 0);
    }

    public ImageBackground(Image image, int layout) {
        this.image = null;
        this.layout = 3;
        this.specifiedImageWidth = -1.0D;
        this.specifiedImageHeight = -1.0D;
        setImage(image);
        setLayout(layout);
    }

    public Image getImage() {
        return this.image;
    }

    public void setImage(Image image) {
        this.image = image;
    }

    public int getLayout() {
        return this.layout;
    }

    public void setLayout(int layout) {
        this.layout = layout;
    }

    public double getSpecifiedImageWidth() {
        return this.specifiedImageWidth;
    }

    public void setSpecifiedImageWidth(double width) {
        this.specifiedImageWidth = width;
    }

    public double getSpecifiedImageHeight() {
        return this.specifiedImageHeight;
    }

    public void setSpecifiedImageHeight(double height) {
        this.specifiedImageHeight = height;
    }

    @Override
    public void paint(Graphics g, Shape shape) {
        if (getImage() == null) {
            return;
        }
        // Rectangle2D rect2D = shape.getBounds2D();
        // Graphics2D g2D = (Graphics2D) g;
        // Shape localShape = g2D.getClip();
        // g2D.clip(shape);
        // g2D.translate(rect2D.getX(), rect2D.getY());
        // GraphHelper.paintImage(g2D, (int) rect2D.getWidth(), (int)
        // rect2D.getHeight(), getImage(), null, getLayout(), (int)
        // getSpecifiedImageWidth(), (int) getSpecifiedImageHeight());
        // g2D.translate(-rect2D.getX(), -rect2D.getY());
        // g2D.setClip(localShape);
    }

    public void paint2(Graphics g, Shape shape, int paramInt1, int paramInt2) {
        if (getImage() == null) {
            return;
        }
        logger.error("not implemented");
        // Rectangle2D rect2D = shape.getBounds2D();
        // Graphics2D g2D = (Graphics2D) g;
        // Shape localShape = g2D.getClip();
        // g2D.clip(shape);
        // g2D.translate(rect2D.getX(), rect2D.getY());
        // GraphHelper.paintImage2(g2D, (int) rect2D.getWidth(), (int)
        // rect2D.getHeight(), getImage(), null, getLayout(), (int)
        // getSpecifiedImageWidth(), (int) getSpecifiedImageHeight(), paramInt1,
        // paramInt2);
        // g2D.translate(-rect2D.getX(), -rect2D.getY());
        // g2D.setClip(localShape);
    }

    private void readObject(ObjectInputStream inputStream) throws ClassNotFoundException, IOException {
        inputStream.defaultReadObject();
        logger.error("not implemented");
        // Object object = inputStream.readObject();
        // if (object == null) {
        // return;
        // }
        // this.image = ((ImageSerializable) object).getImage();
    }

    private void writeObject(ObjectOutputStream outputstream) throws IOException {
        outputstream.defaultWriteObject();
        logger.error("not implemented");
        // if (this.image == null) {
        // return;
        // }
        // ImageSerializable s = new ImageSerializable(this.image);
        // outputstream.writeObject(s);
    }

    public boolean equals(Object object) {
        if (!(object instanceof ImageBackground)) {
            return false;
        }
        ImageBackground localImageBackground = (ImageBackground) object;
        return ((getLayout() == localImageBackground.getLayout())
                && (ComparatorUtils.imageEquals(getImage(), localImageBackground.getImage()))
                && (getSpecifiedImageWidth() == localImageBackground.getSpecifiedImageWidth()) && (getSpecifiedImageHeight() == localImageBackground
                .getSpecifiedImageHeight()));
    }

}
